# Automake file

ACLOCAL_AMFLAGS = -I m4

# Nonstandard package files for distribution
EXTRA_DIST = \
  README.md \
  CHANGELOG \
  LICENSE

# We define the global AM_CPPFLAGS as everything we compile includes from these
# directories. Adding /opt/local/include for Macs using MacPorts.
AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/metadata -I$(top_srcdir)/produce -I$(top_srcdir)/fetch -I$(top_srcdir)/offset -I/opt/local/include

# Build rules for libraries.
lib_LTLIBRARIES = lib/libkafka.la

lib_libkafka_la_SOURCES = \
  lib/src/ApiConstants.cc \
  lib/src/Client.cc \
  lib/src/Connection.cc \
  lib/src/Util.cc \
  lib/src/Packet.cc \
  lib/src/Request.cc \
  lib/src/RequestOrResponse.cc \
  lib/src/Response.cc \
  lib/src/Message.cc \
  lib/src/MessageSet.cc \
  lib/src/TopicNameBlock.h \
  lib/src/metadata/Broker.cc \
  lib/src/metadata/MetadataRequest.cc \
  lib/src/metadata/MetadataResponse.cc \
  lib/src/metadata/PartitionMetadata.cc \
  lib/src/metadata/TopicMetadata.cc \
  lib/src/produce/ProduceRequest.cc \
  lib/src/produce/ProduceMessageSet.cc \
  lib/src/produce/ProduceResponse.cc \
  lib/src/produce/ProduceResponsePartition.cc \
  lib/src/fetch/FetchRequest.cc \
  lib/src/fetch/FetchPartition.cc \
  lib/src/fetch/FetchResponse.cc \
  lib/src/fetch/FetchResponsePartition.cc \
  lib/src/offset/OffsetRequest.cc \
  lib/src/offset/OffsetPartition.cc \
  lib/src/offset/OffsetResponse.cc \
  lib/src/offset/OffsetResponsePartition.cc

LIBKAFKA_LIBRARY_VERSION = 0:5:0
lib_libkafka_la_CPPFLAGS = $(AM_CPPFLAGS) $(COVERAGE_CFLAGS)
lib_libkafka_la_LDFLAGS = -version-info $(LIBKAFKA_LIBRARY_VERSION) $(COVERAGE_LDFLAGS)

lib_libkafka_ladir = $(includedir)/libkafka

pkginclude_HEADERS = \
  lib/src/ApiConstants.h \
  lib/src/Client.h \
  lib/src/Connection.h \
  lib/src/Debug.h \
  lib/src/Packet.h \
  lib/src/PacketWriter.h \
  lib/src/Request.h \
  lib/src/RequestOrResponse.h \
  lib/src/Response.h \
  lib/src/Message.h \
  lib/src/MessageSet.h \
  lib/src/TopicNameBlock.h \
  lib/src/Util.h \
  lib/src/WireFormatter.h \
  lib/src/ErrorHandler.h

metadataincludedir = $(includedir)/libkafka/metadata
dist_metadatainclude_HEADERS = \
  lib/src/metadata/MetadataRequest.h \
  lib/src/metadata/MetadataResponse.h \
  lib/src/metadata/Broker.h \
  lib/src/metadata/PartitionMetadata.h \
  lib/src/metadata/TopicMetadata.h

produceincludedir = $(includedir)/libkafka/produce
dist_produceinclude_HEADERS = \
  lib/src/produce/ProduceRequest.h \
  lib/src/produce/ProduceMessageSet.h \
  lib/src/produce/ProduceResponse.h \
  lib/src/produce/ProduceResponsePartition.h

fetchincludedir = $(includedir)/libkafka/fetch
dist_fetchinclude_HEADERS = \
  lib/src/fetch/FetchRequest.h \
  lib/src/fetch/FetchPartition.h \
  lib/src/fetch/FetchResponse.h \
  lib/src/fetch/FetchResponsePartition.h

offsetincludedir = $(includedir)/libkafka/offset
dist_offsetinclude_HEADERS = \
  lib/src/offset/OffsetRequest.h \
  lib/src/offset/OffsetPartition.h \
  lib/src/offset/OffsetResponse.h \
  lib/src/offset/OffsetResponsePartition.h

# Bulid rules for samples and tests. Automake's naming for some of
# these variables isn't terribly obvious, so this is a brief
# reference:
#
# TESTS -- Programs run automatically by "make check"
# check_PROGRAMS -- Programs built by "make check" but not necessarily run

TESTS=
check_PROGRAMS=

# libkafka unit tests
TESTS += test/libkafka_unit_tests
check_PROGRAMS += test/libkafka_unit_tests
test_libkafka_unit_tests_SOURCES = \
  test/src/BaseTest.cc \
  test/src/ConnectionTest.cc \
  test/src/BrokerTest.cc \
  test/src/MetadataRequestTest.cc \
  test/src/MetadataResponseTest.cc \
  test/src/PartitionMetadataTest.cc \
  test/src/TopicMetadataTest.cc \
  test/src/PacketTest.cc \
  test/src/MessageTest.cc \
  test/src/MessageSetTest.cc \
  test/src/RequestOrResponseTest.cc \
  test/src/RequestTest.cc \
  test/src/ResponseTest.cc \
  test/src/TopicNameBlockTest.cc \
  test/src/ProduceRequestTest.cc \
  test/src/ProduceMessageSetTest.cc \
  test/src/ProduceResponseTest.cc \
  test/src/ProduceResponsePartitionTest.cc \
  test/src/FetchRequestTest.cc \
  test/src/FetchPartitionTest.cc \
  test/src/FetchResponseTest.cc \
  test/src/FetchResponsePartitionTest.cc \
  test/src/OffsetRequestTest.cc \
  test/src/OffsetPartitionTest.cc \
  test/src/OffsetResponseTest.cc \
  test/src/OffsetResponsePartitionTest.cc \
  test/src/ApiConstantsTest.cc \
  test/src/ClientTest.cc \
  test/src/TestConfig.cc \
  test/src/TestConfig.h \
  test/src/BaseTest.h
test_libkafka_unit_tests_CPPFLAGS = $(GTEST_CPPFLAGS) -I$(srcdir)/lib/src
test_libkafka_unit_tests_LDFLAGS = $(GTEST_LDFLAGS)
test_libkafka_unit_tests_LDADD = lib/libkafka.la $(GTEST_LIBS) -lz -lsnappy

# Deletes all the files generated by autogen.sh.
MAINTAINERCLEANFILES = \
  aclocal.m4 \
  configure \
  Makefile.in \
  config/compile \
  config/config.guess \
  config/config.h.in \
  config/config.h.in~ \
  config/config.sub \
  config/depcomp \
  config/install-sh \
  config/ltmain.sh \
  config/missing \
  config/test-driver \
  mkinstalldirs \
  stamp.h.in \
  m4/ltsugar.m4 \
  m4/libtool.m4 \
  m4/ltversion.m4 \
  m4/lt~obsolete.m4 \
  m4/ltoptions.m4 \
  lib/src/*gcno lib/src/*/*gcno

APP_NAME=libkafka

# Create a deb package
DEB_DIR=deb
deb: dist
	- rm -r $(DEB_DIR)
	mkdir -p $(DEB_DIR)
	cp $(APP_NAME)-$(VERSION).tar.bz2 $(DEB_DIR)/
	tar --directory $(DEB_DIR)/ -xjf $(DEB_DIR)/$(APP_NAME)-$(VERSION).tar.bz2
#cd $(DEB_DIR)/$(APP_NAME)-$(VERSION)/; ./configure --prefix=$(PWD) && make
#echo | dh_make --single --copyright apache2 -e tompkins@adobe.com -f ../$(APP_NAME)-$(VERSION).tar.bz2
#cp CHANGELOG install/deb/control install/deb/copyright $(DEB_DIR)/$(APP_NAME)-$(VERSION)/debian/
#cd $(DEB_DIR)/$(APP_NAME)-$(VERSION)/; rm debian/README.Debian debian/*.ex debian/*.EX; ./configure; dpkg-buildpackage -rfakeroot; mv ../*.deb $(PWD)
#rm -r $(DEB_DIR);

# Create an RPM package
RPM_DIR=rpm
rpm: dist
	mkdir -p $(RPM_DIR)/RPMS
	mkdir -p $(RPM_DIR)/SRPMS
	mkdir -p $(RPM_DIR)/BUILD
	mkdir -p $(RPM_DIR)/SOURCES
	mkdir -p $(RPM_DIR)/tmp
	- rm -r $(RPM_DIR)/BUILD/$(APP_NAME)-root
	- rm -r $(RPM_DIR)/RPMS/$(APP_NAME)-*
	- rm -r $(RPM_DIR)/SRPMS/$(APP_NAME)-*
	- rm -r $(RPM_DIR)/SOURCES/$(APP_NAME)-*
	cp $(APP_NAME)-$(VERSION).tar.bz2 $(RPM_DIR)/SOURCES/
	rpmbuild -vv --nodeps -bb install/rpm/libkafka.spec

rpm-clean: distclean maintainer-clean
	- rm -rf $(RPM_DIR) 
	- rm -rf $(DEB_DIR) 
	- rm -rf libkafka-*gz
	- rm -rf libkafka-*bz2
	- rm -rf libkafka-*zip

# Coverage targets

if HAVE_GCOV

.PHONY: clean-gcda
clean-gcda:
	@echo Removing old coverage results
	-find -name '*.gcda' -print | xargs -r rm

.PHONY: coverage-html generate-coverage-html clean-coverage-html
#coverage-html: clean-gcda
coverage-html:
	-$(MAKE) $(AM_MAKEFLAGS) -k check
	$(MAKE) $(AM_MAKEFLAGS) generate-coverage-html

generate-coverage-html:
	@echo Collecting coverage data with lcov
	$(LCOV) --gcov-tool $(GCOV) -b . --directory $(top_builddir) --capture --output-file coverage.info --no-checksum --compat-libtool
	$(LCOV) --remove coverage.info "/usr*" -o coverage.info
	$(LCOV) --remove coverage.info "/opt*" -o coverage.info
	LANG=C++ $(GENHTML) --prefix $(top_builddir) --output-directory coveragereport --title "Code Coverage" --legend --show-details coverage.info

clean-coverage-html: clean-gcda
	-$(LCOV) --directory $(top_builddir) -z
	-rm -rf coverage.info coveragereport

clean-local: clean-coverage-html

endif # HAVE_GCOV
